package ai.people.netmon.userconfig.service.handle.impl;

import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.domain.userconfig.entity.UserCollectConfigAccount;
import ai.people.netmon.userconfig.service.handle.HandleUrlService;
import ai.people.netmon.userconfig.service.handle.HandleUrlUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 说明
 *
 * @author zhenglin
 * @since 2022/3/25
 */
@Service
public class YoutubeHandleUrlServiceImpl implements HandleUrlService, InitializingBean {
    private static final Pattern YOUTUBE_PATTERN = Pattern.compile("https?://www\\.youtube\\.com/(channel)?(c)?(user)?/?(.+)/");

    @Override
    public void afterPropertiesSet() throws Exception {
        HandleUrlUtil.putService(AnalysisSourceEnum.youtube.name(), this);
    }

    @Override
    public String handleUrl(UserCollectConfigAccount account) {
        String text = account.getAccountLink();
        Matcher matcher = YOUTUBE_PATTERN.matcher(text);
        if (matcher.find()) {
            String group = matcher.group(4);
            return handle2(group);
        }
        return null;
    }
}
